//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows.Input;
using OpenLS.Spreadsheet.Internals;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI
{
    public class SpreadsheetEditingCommands
    {
        internal static RoutedUICommand SetFontFamily = new RoutedUICommand("SetFontFamily", "SetFontFamily", typeof(SpreadsheetEditingCommands));
        internal static RoutedUICommand SetFontSize = new RoutedUICommand("SetFontSize", "SetFontSize", typeof(SpreadsheetEditingCommands));
        private static readonly RoutedUICommand[] _internalCommands = new RoutedUICommand[(int)CommandId.Last];


        public static RoutedUICommand SelectDownByBlock
        {
            get { return EnsureCommand(CommandId.SelectDownByBlock); }
        }

        public static RoutedUICommand SelectLeftByBlock
        {
            get { return EnsureCommand(CommandId.SelectLeftByBlock); }
        }

        public static RoutedUICommand SelectRightByBlock
        {
            get { return EnsureCommand(CommandId.SelectRightByBlock); }
        }

        public static RoutedUICommand SelectUpByBlock
        {
            get { return EnsureCommand(CommandId.SelectUpByBlock); }
        }

        
        public static RoutedUICommand MoveDownByBlock
        {
            get { return EnsureCommand(CommandId.MoveDownByBlock); }
        }

        public static RoutedUICommand MoveLeftByBlock
        {
            get { return EnsureCommand(CommandId.MoveLeftByBlock); }
        }

        public static RoutedUICommand MoveRightByBlock
        {
            get { return EnsureCommand(CommandId.MoveRightByBlock); }
        }

        public static RoutedUICommand MoveUpByBlock
        {
            get { return EnsureCommand(CommandId.MoveUpByBlock); }
        }

        public static RoutedUICommand ToggleEndMode
        {
            get { return EnsureCommand(CommandId.ToggleEndMode); }
        }
        internal static RoutedUICommand ToggleLockReference
        {
            get { return EnsureCommand(CommandId.ToggleLockReference); }
        }

        public static RoutedUICommand ClearAll
        {
            get { return EnsureCommand(CommandId.ClearAll); }
        }

        public static RoutedUICommand ClearFormats
        {
            get { return EnsureCommand(CommandId.ClearFormat); }
        }

        public static RoutedUICommand ClearContents
        {
            get { return EnsureCommand(CommandId.ClearContents); }
        }
        public static RoutedUICommand ClearComments
        {
            get { return EnsureCommand(CommandId.ClearComments); }
        }
        public static RoutedUICommand ClearHyperlinks
        {
            get { return EnsureCommand(CommandId.ClearHyperlink); }
        }

        public static RoutedCommand DeleteBackgroundPicture
        {
            get { return EnsureCommand(CommandId.DeleteBackgroundPicture); }
        }

        public static RoutedCommand SetBackgroundPicture
        {
            get { return EnsureCommand(CommandId.SetBackgroundPicture); }
        }


        public static RoutedCommand SetFormat
        {
            get { return EnsureCommand(CommandId.SetFormat); }
        }

        public static RoutedCommand ToggleTextWrapping
        {
            get { return EnsureCommand(CommandId.WrapText); }
        }


        public static RoutedUICommand MergeCells
        {
            get { return EnsureCommand(CommandId.MergeCells); }
        }

        public static RoutedUICommand ShrinkToFit
        {
            get { return EnsureCommand(CommandId.ShrinkToFit); }
        }

        public static RoutedCommand UnmergeCells
        {
            get { return EnsureCommand(CommandId.UnmergeCells); }
        }

        // static readonly private RoutedCommand mergeCellsAndCenter = new RoutedUICommand("Merge cells and center", "mergeCellsAndCenter", typeof(SpreadsheetCommands));
        public static RoutedCommand MergeCellsAndCenter
        {
            get { return EnsureCommand(CommandId.MergeCellsAndCenter, "MergeCenter"); }
        }

        public static RoutedCommand MergeCellsAcross
        {
            get { return EnsureCommand(CommandId.MergeCellsAcross); }
        }

        public static RoutedCommand VerticalAlignCenter
        {
            get { return EnsureCommand(CommandId.VerticalAlignCenter); }
        }

        public static RoutedCommand VerticalAlignBottom
        {
            get { return EnsureCommand(CommandId.VerticalAlignBottom); }
        }

        public static RoutedCommand ToggleEditionMode
        {
            get { return EnsureCommand(CommandId.ToggleEditionMode); }
        }

        public static RoutedCommand SelectAll
        {
            get { return EnsureCommand(CommandId.SelectAll); }
        }

        public static RoutedCommand VerticalAlignTop
        {
            get { return EnsureCommand(CommandId.VerticalAlignTop); }
        }

        private static RoutedUICommand EnsureCommand(CommandId commandId)
        {
            return EnsureCommand(commandId, commandId.ToString());
        }

        private static RoutedUICommand EnsureCommand(CommandId idCommand, string propertyName)
        {
            lock (_internalCommands.SyncRoot)
            {
                if (_internalCommands[(int) idCommand] == null)
                {
                    string text = GetUIText(idCommand) ?? "!!Missing" + idCommand;
                    var newCommand = new RoutedUICommand(text, propertyName, typeof (SpreadsheetEditingCommands));
                    _internalCommands[(int) idCommand] = newCommand;
                }
            }
            return _internalCommands[(int) idCommand];
        }

        private static string GetUIText(CommandId idCommand)
        {
            switch (idCommand)
            {
                case CommandId.WrapText:
                    return UIStringTable.WrapText;
                case CommandId.MergeCells:
                    return UIStringTable.MergeCells;
                case CommandId.MergeCellsAndCenter:
                    return UIStringTable.MergeCellsAndCenter;
                case CommandId.ShrinkToFit:
                    return UIStringTable.ShrinkToFit;
                case CommandId.ClearAll:
                    return UIStringTable.ClearAll;
                case CommandId.ClearFormat:
                    return StringTable.ClearFormats;
                case CommandId.ClearContents:
                    return StringTable.ClearContents;
                case CommandId.ClearHyperlink:
                    return StringTable.ClearHyperlinks;
                case CommandId.ClearComments:
                    return StringTable.ClearComments;
                case CommandId.DeleteBackgroundPicture:
                    return UIStringTable.DeleteBackgroundPicture;
                case CommandId.SetBackgroundPicture:
                    return UIStringTable.SetBackgroundPicture;
                case CommandId.SetFormat:
                    return UIStringTable.SetFormat;
                case CommandId.UnmergeCells:
                    return UIStringTable.UnmergeCells;
                case CommandId.MergeCellsAcross:
                    return UIStringTable.MergeCellsAcross;
                case CommandId.VerticalAlignBottom:
                    return UIStringTable.VerticalAlignBottom;
                case CommandId.ToggleEditionMode:
                    return UIStringTable.ToggleEditionMode;
                case CommandId.SelectAll:
                    return UIStringTable.SelectAll;
                case CommandId.VerticalAlignCenter:
                    return UIStringTable.VerticalAlignCenter;
                case CommandId.VerticalAlignTop:
                    return UIStringTable.VerticalAlignTop;

                case CommandId.MoveDownByBlock:
                    return UIStringTable.MoveDownByBlock;
                case CommandId.MoveUpByBlock:
                    return UIStringTable.MoveUpByBlock;
                case CommandId.MoveLeftByBlock:
                    return UIStringTable.MoveLeftByBlock;
                case CommandId.MoveRightByBlock:
                    return UIStringTable.MoveRightByBlock;

                case CommandId.SelectDownByBlock:
                    return UIStringTable.SelectDownByBlock;  
                case CommandId.SelectUpByBlock:
                    return UIStringTable.SelectUpByBlock;
                case CommandId.SelectLeftByBlock:
                    return UIStringTable.SelectLeftByBlock;
                case CommandId.SelectRightByBlock:
                    return UIStringTable.SelectRightByBlock;

                case CommandId.ToggleEndMode:
                    return UIStringTable.ToggleEndMode;
                    case CommandId.ToggleLockReference:
                    return UIStringTable.ToggleLockReference;
                default:
                    throw new NotSupportedException(idCommand.ToString());
            }
        }

        #region Nested type: CommandId

        private enum CommandId
        {
            ToggleLockReference,
            ToggleEndMode,

            MoveUpByBlock,
            MoveDownByBlock,
            MoveLeftByBlock,
            MoveRightByBlock,

            SelectUpByBlock,
            SelectDownByBlock,
            SelectLeftByBlock,
            SelectRightByBlock,

            WrapText,
            ShrinkToFit,
            MergeCells,
            MergeCellsAndCenter,
            ClearAll,
            ClearFormat,
            ClearContents,
            DeleteBackgroundPicture,
            SetBackgroundPicture,
            SetFormat,
            UnmergeCells,
            MergeCellsAcross,
            VerticalAlignCenter,
            VerticalAlignTop,
            VerticalAlignBottom,
            ToggleEditionMode,
            SelectAll,
            ClearHyperlink,
            ClearComments,
            Last,
           
        }

        #endregion
    }
}